package com.wkq.move.module.main.frame.view

import android.content.Context
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.appbar.AppBarLayout
import com.wkq.base.frame.mosby.delegate.MvpView
import com.wkq.lib_base.adapter.KtDataBindingAdapter
import com.wkq.lib_base.utlis.AlertUtil
import com.wkq.lib_base.utlis.StatusBarUtil
import com.wkq.move.R
import com.wkq.move.module.main.ui.adapter.HomeAdapter
import com.wkq.move.module.main.ui.fragment.HomeFragment
import com.wkq.move.module.tv.ui.activity.UsTvShowActivity
import com.wkq.net.model.USTVShowHomeInfo
import com.wkq.net.model.USTVShowInfo

/**
 *
 * 作者:吴奎庆
 *
 * 时间:2020/5/15
 *
 * 用途:
 */

class HomeView : MvpView {

    var mFragment: HomeFragment
    var mAdapter: HomeAdapter? = null
    var newsAdapter: HomeAdapter? = null

    constructor(mFragment: HomeFragment) {
        this.mFragment = mFragment
    }


    fun initView() {
        mFragment.binding.appbarLayout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
            if (Math.abs(verticalOffset) >= appBarLayout.totalScrollRange) {
                if (!mFragment.isExpend) {
                    StatusBarUtil.setDarkMode(mFragment.activity)
                    mFragment.isExpend = !mFragment.isExpend
                    mFragment.binding.toolbar.visibility = View.VISIBLE
                    mFragment.binding.tvTitle.visibility = View.VISIBLE
                }
            } else {
                if (mFragment.isExpend) {
                    StatusBarUtil.setDarkMode(mFragment.activity)
                    mFragment.isExpend = !mFragment.isExpend
                    mFragment.binding.toolbar.visibility = View.GONE
                    mFragment.binding.tvTitle.visibility = View.GONE
                }
            }
        })

        mAdapter = HomeAdapter(mFragment.activity as Context, R.layout.item_home)
        newsAdapter = HomeAdapter(mFragment.activity as Context, R.layout.item_home)

        mFragment.binding.rvMovies.layoutManager = GridLayoutManager(mFragment.activity, 3)
        mFragment.binding.rvNews.layoutManager = GridLayoutManager(mFragment.activity, 3)
        mFragment.binding.rvMovies.adapter = mAdapter
        mFragment.binding.rvNews.adapter = newsAdapter

        mAdapter!!.setOnViewClickListener(object :
            KtDataBindingAdapter.OnAdapterViewClickListener<USTVShowInfo> {
            override fun onViewClick(v: View?, program: USTVShowInfo?) {
                UsTvShowActivity.Companion.newInstance(
                    mFragment.activity as Context,
                    program?.tvSite
                )
            }
        })
        newsAdapter!!.setOnViewClickListener(object :
            KtDataBindingAdapter.OnAdapterViewClickListener<USTVShowInfo> {
            override fun onViewClick(v: View?, program: USTVShowInfo?) {
                UsTvShowActivity.Companion.newInstance(
                    mFragment.activity as Context,
                    program?.tvSite
                )
            }
        })


    }

    fun initData(info: USTVShowHomeInfo?) {
        if (info?.hotList?.toMutableList() != null && info?.hotList?.size!! > 0) {
            mFragment.binding.tvHot.visibility = View.VISIBLE
            mAdapter?.addItems(info?.hotList?.toMutableList())
        }

        if (info?.newUpdateList?.toMutableList() != null && info?.newUpdateList?.size!! > 0) {
            mFragment.binding.tvUpdates.visibility = View.VISIBLE
            newsAdapter?.addItems(info?.newUpdateList?.toMutableList())
        }

    }

    fun showMessage(message: String?) {
        if (mFragment == null || mFragment.activity == null || TextUtils.isEmpty(message)) return;
        AlertUtil.showDeftToast(mFragment.activity, message)

    }

}


